package bitoperations;
//不用➗的除法
public class Div {
    public static void main(String[] args) {
        System.out.println(div(4, -2));
    }

    public static int div(int a, int b) {
        int x = isNeg(a) ? negNum(a) : a;
        int y = isNeg(b) ? negNum(b) : b;
        int res = 0;
        for(int i = 31; i > -1; i = Minus.minus(i, 1)){
            if((x >> i) >= y){
                res |= (1 << i);
                x = Minus.minus(x,y << i);
            }
        }
        return isNeg(a)^isNeg(b) ? negNum(res) : res;
    }

    private static int negNum(int a) {
        return ~a + 1;
    }

    public static boolean isNeg(int n){
        return n < 0;
    }


}
